<template>
  <div class="login-pwd">
    <h3 class="login__title">
      <img :src="logoIcon" alt="" />
      <span>欢迎登录</span>
    </h3>
    <div class="login__type">
      <div class="type--active">账号密码</div>
      <div>短信密码</div>
    </div>
    <el-form :rules="rules" :model="useForm" inline-message>
      <el-form-item prop="username">
        <el-input
          v-model="useForm.username"
          class="w-50 m-2"
          size="large"
          placeholder="手机/邮箱"
          @focus="formFlag.userNameFlag = true"
        >
          <template #prefix>
            <i class="iconfont ic-worker-fill"></i>
          </template>
          <template #suffix>
            <i
              v-show="formFlag.userNameFlag"
              :class="`iconfont ${formFlag.userNameIcon}`"
            ></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="useForm.password"
          class="w-50 m-2"
          size="large"
          placeholder="密码"
          show-password
          type="password"
          @focus="formFlag.pwdFlag = true"
        >
          <template #prefix>
            <i class="iconfont ic-lock"></i>
          </template>
          <template #suffix>
            <i
              v-show="formFlag.pwdFlag"
              :class="`iconfont ${formFlag.pwdIcon}`"
            ></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <div
          class="login__btn"
          :class="{ 'login__btn--active': formFlag.submitBtn }"
          @click="submit"
        >
          登录
        </div>
      </el-form-item>
    </el-form>
    <el-checkbox v-model="checked1" label="记住密码" size="small"></el-checkbox>
    <div class="login__forget-pwd">忘记密码？</div>
  </div>
</template>

<script setup>
import { validatorMobile } from "@/utils/regular";
import { reactive, ref, watch, watchEffect } from "vue";
import loginStore from "@/stores/login";
import logo from "@/assets/images/logo.png";
import logoIcon from "@/assets/images/logo_icon.png";
</script>
<script>
export default {
  data() {
    return {
      formFlag: {
        userNameFlag: false,
        userNameIcon: "",
        pwdFlag: false,
        pwdIcon: "",
        submitBtn: false,
      },
      useForm: {},
      useLogin: {},
      rules: {},
    };
  },
  mounted() {
    this.useLogin = loginStore();
    this.useForm = { ...this.useLogin.form };
    if (this.useForm.username && this.useForm.password) {
      this.formFlag.submitBtn = true;
      this.validateMobile("", this.useForm.username, () => {});
      this.validatePwd("", this.useForm.password, () => {});
    } else {
      this.formFlag.submitBtn = false;
    }
    this.rules = {
      username: [
        {
          trigger: "change",
          validator: this.validateMobile,
        },
      ],
      password: [
        {
          trigger: "change",
          validator: this.validatePwd,
        },
      ],
    };
  },
  watch: {
    formFlag: {
      handler(newValue, oldValue) {
        if (
          newValue.userNameIcon == "ic-circle-correct-fill" &&
          newValue.pwdIcon == "ic-circle-correct-fill"
        ) {
          this.formFlag.submitBtn = true;
        } else {
          this.formFlag.submitBtn = false;
        }
      },
      deep: true,
      setImmediate: true,
    },
  },
  methods: {
    submit() {
      if (this.formFlag.submitBtn) {
        this.useLogin.login();
      }
    },
    validateMobile(rule, value, callback) {
      if (value === "") {
        this.formFlag.userNameIcon = "ic-circle-wrong-fill";
        callback(new Error("请输入手机号"));
      } else {
        if (!validatorMobile(value)) {
          this.formFlag.userNameIcon = "ic-circle-wrong-fill";
          callback(new Error("请输入正确的手机号"));
        } else {
          this.formFlag.userNameIcon = "ic-circle-correct-fill";
          callback();
        }
      }
    },
    validatePwd(rule, value, callback) {
      if (value === "") {
        this.formFlag.pwdIcon = "ic-circle-wrong-fill";
        callback(new Error("请输入密码"));
      } else {
        this.formFlag.pwdIcon = "ic-circle-correct-fill";
        callback();
      }
    },
  },
};
</script>
<style lang="scss">
.login-pwd {
  width: 520px;
  height: 550px;
  border-radius: 8px;
  padding: 64px 80px 40px;
  --el-color-primary: #6042dc;
}
.login {
  &__title {
    font-size: 32px;
    margin-bottom: 58px;
    @include flex-y-center;
    img {
      margin-right: 16px;
      width: 40px;
      height: 40px;
    }
  }
  &__type {
    display: flex;
    margin-bottom: 24px;
    align-items: baseline;
    div {
      margin-right: 24px;
      color: map-get($font-sizes, text);
      font-size: 16px;
      cursor: pointer;
    }
    .type {
      &--active {
        font-size: 18px;
        color: map-get($font-sizes, title);
      }
    }
  }
  .el-form-item {
    --el-color-white: #f2f3f5;
    margin-bottom: 16px;
    .el-input__inner {
      border: none;
      color: map-get($font-sizes, title);
    }
    .el-input__prefix {
      left: 12px;
    }
    i {
      height: 40px;
      line-height: 40px;
      color: map-get($font-sizes, text);
    }
    input[type="password"]::-ms-reveal {
      display: none;
    }
    .el-input__validateIcon {
      display: none;
    }
    .ic-circle-correct-fill {
      color: map-get($map: $status-success, $key: static);
    }
    .ic-circle-wrong-fill {
      color: #f53f3f;
    }
  }
  &__btn {
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    border-radius: 2px;
    color: map-get($font-sizes, white);
    background: map-get($bg-primary, disabled);
  }
  &__btn--active {
    background: map-get($bg-primary, active);
    cursor: pointer;
  }
  &__forget-pwd {
    width: 100%;
    color: map-get($font-sizes, smTitle);
    text-align: center;
    @include absolute-left-bottom-0();
    bottom: 40px;
  }
}
</style>
